#!/usr/bin/perl

# $Id: 09_dbwrapperfactory.t 197 2010-07-11 10:59:42Z dolbanavt $
# $Date: 2010-07-12 21:50:31 +0300 (Пнд, 12 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/t/09_dbwrapperfactory.t $
# $LastChangedRevision: 349 $

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
    push @INC, catfile( $RealBin, '..', 'lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Test::More 0.88;
use Const::Fast qw( const );
use Test::MockObject;

const my $MODULE => 'DBWrapperFactory';

const my $SINGLETON_REQUESTED => 0;

use_ok $MODULE;
require_ok $MODULE;

can_ok $MODULE, qw(
    create_hosts_checker_wrapper
    create_crawler_manager_wrapper
    create_builder_wrapper
);

{
    my $mock = Test::MockObject->new();

    $mock->fake_module( 'FactoryMethod',
        _instantiate_class => sub {
            my $self = shift;

            is $self, 'FactoryMethod', 'fake FactoryMethod class';

            my %param = @ARG;

            is $param{'class_name'},
               'DBWrapper::HostsChecker',
               'mocked _instantiate_class(class_name)';

            is $param{'singleton_requested'},
               $SINGLETON_REQUESTED,
               'mocked _instantiate_class(singleton_requested)';

            my %arguments = @{ $param{'constructor_arguments'} };

            is $arguments{'dbh'},
               'preved',
               'DBWrapper::HostsChecker( dbh )';

            return bless {}, 'DBWrapper::HostsChecker';
        },
    );

    my $w = $MODULE->create_hosts_checker_wrapper(
        dbh => 'preved',
    );

    is ref $w,
       'DBWrapper::HostsChecker',
       'got DBWrapper::HostsChecker object';
}

{
    my $mock = Test::MockObject->new();

    $mock->fake_module( 'FactoryMethod',
        _instantiate_class => sub {
            my $self = shift;

            is $self, 'FactoryMethod', 'fake FactoryMethod class';

            my %param = @ARG;

            is $param{'class_name'},
               'DBWrapper::CrawlerManager',
               'mocked _instantiate_class(class_name)';

            is $param{'singleton_requested'},
               $SINGLETON_REQUESTED,
               'mocked _instantiate_class(singleton_requested)';

            my %arguments = @{ $param{'constructor_arguments'} };

            is $arguments{'dbh'},
               'preved',
               'DBWrapper::CrawlerManager( dbh )';

            return bless {}, 'DBWrapper::CrawlerManager';
        },
    );

    my $w = $MODULE->create_crawler_manager_wrapper(
        dbh => 'preved',
    );

    is ref $w,
       'DBWrapper::CrawlerManager',
       'got DBWrapper::CrawlerManager object';
}

{
    my $mock = Test::MockObject->new();

    $mock->fake_module( 'FactoryMethod',
        _instantiate_class => sub {
            my $self = shift;

            is $self, 'FactoryMethod', 'fake FactoryMethod class';

            my %param = @ARG;

            is $param{'class_name'},
               'DBWrapper::DatabaseBuilder',
               'mocked _instantiate_class(class_name)';

            is $param{'singleton_requested'},
               $SINGLETON_REQUESTED,
               'mocked _instantiate_class(singleton_requested)';

            my %arguments = @{ $param{'constructor_arguments'} };

            is $arguments{'dbh'},
               'preved',
               'DBWrapper::DatabaseBuilder( dbh )';

            return bless {}, 'DBWrapper::DatabaseBuilder';
        },
    );

    my $w = $MODULE->create_builder_wrapper(
        dbh => 'preved',
    );

    is ref $w,
       'DBWrapper::DatabaseBuilder',
       'got DBWrapper::DatabaseBuilder object';
}

done_testing();
